Page({
  data: {
    problem: null,
    problemid: 0,
    replies: [],
    showReplyInput: false,
    inputFocus: false,
    replyContent: '',
    currentCommentId: null
  },

  onLoad(options) {
    console.log(options);
    wx.request({
      url: 'http://localhost:8080/consult/list/' + options.id,
      success: (res) => {
        console.log(res.data.problem);
        console.log(res.data.replyList);
        this.setData({
          problemid: options.id,
          problem: res.data.problem,
          replies: res.data.replyList
        })
      }
    })
  },

  onCommentTap(e) {
    const commentId = e.currentTarget.dataset.commentId;
    this.setData({
      showReplyInput: true,
      currentCommentId: commentId
    });
    setTimeout(() => {
      this.setData({ inputFocus: true });
    }, 100);
  },

  handleCommentProblem(e) {
    this.setData({
      showReplyInput: true
    })


  },
  // private Long rId;
  // private String replyText;
  // private long problemId;
  // private LogonUser user;
  // private LogonUser parentUser;
  // private Long userId;
  // private Long parentReplyId;
  // private Integer rootId;
  // private List<Reply> replyList = new ArrayList<>();

  handlComment(e) {
    wx.request({
      url: 'http://localhost:8080/consult/problem/comment',
      method: "POST",
      data: {
        replyText: this.data.replyContent,
        userId: 3,
        problemId: this.data.problemid,
        parentReplyId: null
      },
      success: res => {
        wx.request({
          url: `http://localhost:8080/consult/list/${this.data.problemid}`,
          success: (res) => {
            this.setData({
              problem: res.data.problem,
              replies: res.data.replyList
            })
          }
        })
      }
    })
  },
  focusInput() {
    wx.createSelectorQuery()
      .select('#replyInput')
      .fields({
        dataset: true,
        size: true,
        scrollOffset: true,
        properties: ['scrollX', 'scrollY'],
        computedStyle: ['margin', 'backgroundColor'],
        context: true,
      }, function (res) {
        res.node.focus({
          success: function () {
            console.log('键盘弹出成功');
          },
          fail: function (err) {
            console.log('键盘弹出失败', err);
          }
        });
      }).exec();
  },

  showKeyboard() {
    if (wx.showKeyboard) {
      wx.showKeyboard({
        success: () => console.log('键盘弹出成功'),
        fail: (err) => console.log('键盘弹出失败', err)
      });
    } else {
      console.log('wx.showKeyboard 不可用');
    }
  },

  onReplyInput(e) {
    this.setData({
      replyContent: e.detail.value
    });
  },

  onReplyInputBlur() {
    this.setData({
      showReplyInput: false,
      inputFocus: false
    });
  },

  onReplySubmit() {
    const { replyContent, currentCommentId } = this.data;
    if (replyContent.trim()) {
      console.log(`回复评论 ${currentCommentId}: ${replyContent}`);
      this.setData({
        replyContent: '',
        showReplyInput: false,
        inputFocus: false,
        currentCommentId: null
      });
    }
  },

  // 其他生命周期函数
  onReady() { },
  onShow() { },
  onHide() { },
  onUnload() { },
  onPullDownRefresh() { },
  onReachBottom() { },
  onShareAppMessage() { }
})